package academic.ui.client.widgets.forms;

import ui.client.GenericContainerTag;
import ui.client.widgets.InputField;
import ui.client.widgets.TextAreaInput;

import academic.ui.client.jquery.JQuery;
import academic.ui.client.rules.Rules;

import com.google.gwt.event.dom.client.HasKeyDownHandlers;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.HasText;

public class BuyAsGiftForm extends FormWidget {

	
	

	private InputField email = new InputField("Email");
	private TextAreaInput message = new TextAreaInput("message","emailmsg");
	private InputField senddate = new InputField("Send Your Gift On This Date","senddate","right now");
	private KeyDownHandler removeErrors = new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			JQuery.fadeOutErrors();
		}
		
	};
	
	public BuyAsGiftForm() {
		super();
		div.setStyleName("buyAsGiftForm");
		div.add(email);
		div.add(message);
		div.add(senddate);
		senddate.getField().setStyleName("datepicker");
		email.getField().setStyleName("longInput");
		message.setStyleName("longInput");		
		div.add(errorMsgs);
		email.getField().addKeyDownHandler(removeErrors);
		message.getField().addKeyDownHandler(removeErrors);
	}
	
	@Override
	public Boolean checkRules() {
		errorMsgs.clear();
		if("".equals(email.getText()))
		{
			email.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Email is required."));
		}
		if("".equals(message.getText()))
		{
			message.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Password is required."));
		}
		if(!"".equals(email.getText()) && !Rules.validEmail(email.getText()))
		{
			email.addStyleName("invalid");
			errorMsgs.add(new GenericContainerTag("div","Email is not valid."));
		}
		return errorMsgs.getWidgetCount()<1; 
	}

	@Override
	public void removeErrors() {
		errorMsgs.clear();
		email.removeStyleName("missingValue");
		message.removeStyleName("missingValue");
		email.removeStyleName("invalid");
	}

	public HasText getEmail() {
		return email;
	}

	public HasText getMessage() {
		return message;
	}

	public HasKeyDownHandlers getEmailKeyDown() {
		return email.getField();
	}

	public void reset() {
		removeErrors();
		email.setText("");
		email.getField().setEnabled(true);
		message.setText("");
		message.getField().setEnabled(true);
	}

	public HasKeyDownHandlers getMessageKeyDown() {
		return message.getField();
	}

	public String getSendDate() {
		return senddate.getField().getText();
	}

}
